<?php
declare (strict_types=1);

namespace app\controller;

use app\middleware\Auth as AuthMiddleware;
use think\exception\ValidateException;
use think\facade\View;
use think\Request;
use app\model\User as UserModel;
use app\validate\User as UserValidate;

class User
{

    protected $middleware = [AuthMiddleware::class];

//    提示模版路径
    private $toast = 'public/toast';

    /**
     * 显示资源列表
     *
     * @return string
     *
     */
    public function index()
    {
        return View::fetch('index', [
            'list' => UserModel::withsearch([
                'gender', 'username', 'email', 'create_time'
            ], [
                'gender' => \request()->param('gender'),
                'username' => \request()->param('username'),
                'email' => Request()->param('email'),
                'create_time' => \request()->param('create_time')
            ])->paginate([
                'list_rows' => 5,
                'query' => \request()->param()
            ]),
            'orderTime' => \request()->param('create_time') == 'desc' ? 'asc' : 'desc'
        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return string
     */
    public function create()
    {
        //等于view::fetch('create')
        return View::fetch();
    }


    public function save(Request $request)
    {
        $data = $request->param();

        try {
            validate(UserValidate::class)->batch(true)->check($request->param());
        } catch (ValidateException $exception) {
            return view($this->toast, [
                'infos' => $exception->getError(),
                'url_text' => '返回上一页',
                'url_path' => url('/user/create')
            ]);
        }

        //sha1加密
        $data['password'] = sha1($data['password']);
        //写入数据库
        $id = UserModel::create($data)->getData('id');

//        喜好保存
        UserModel::find($id)->hobby()->save([
            'content' => $data['content'],
        ]);

//        $hobby = UserModel::find($id);
//        $hobby->hobby->save(['content' => $data['content']]);

        //返回
        return $id ? view($this->toast, [
            'infos' => ['恭喜,注册成功~'],
            'url_text' => '去首页',
            'url_path' => url('/')
        ]) : '注册失败';

    }


    public function read($id)
    {
        //
    }


    public function edit($id)
    {
        return View::fetch('edit', [
            'obj' => UserModel::find($id)
        ]);
    }


    public function update(Request $request, $id)
    {
        $data = $request->param();

        try {
            validate(UserValidate::class)->batch(true)
                ->scene('edit')
                ->check($request->param());
        } catch (ValidateException $exception) {
            return view($this->toast, [
                'infos' => $exception->getError(),
                'url_text' => '返回上一页',
                'url_path' => url('/user/' . $id . '/edit')
            ]);
        }

        if (!empty($data['newpasswordnot'])) {
            $data['password'] = sha1($data['newpasswordnot']);
        }

        return UserModel::update($data) ? view($this->toast, [
            'infos' => ['恭喜,修改成功~'],
            'url_text' => '去首页',
            'url_path' => url('/')
        ]) : '修改失败';
    }


    public function delete($id)
    {
        return UserModel::destroy($id) ? view($this->toast, [
            'infos' => ['删除成功的说!'],
            'url_text' => '去首页',
            'url_path' => url('/')
        ]) : '删除失败';
    }
}
